package com.caishi.lkx.exam.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.caishi.lkx.exam.mapper.ProductBaseMapper;
import com.caishi.lkx.exam.model.ColumnModel;
import com.caishi.lkx.exam.model.ProductBaseModel;
import com.caishi.lkx.exam.service.IColumnService;
import com.caishi.lkx.exam.service.IProductBaseService;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.exception.BizRuntimeException;
import com.caishi.lkx.exam.ExamResultCode;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductBaseServiceImpl implements IProductBaseService {

    @Resource
    private ProductBaseMapper productBaseMapper;
    @Resource
    private IColumnService columnService;

    @Override
    public IBaseMapper<ProductBaseModel, String> getMapper() {
        return productBaseMapper;
    }

    @Override
    public List<ColumnModel> getEfficitiveColums(String productId) {
        ProductBaseModel product = this.getById(productId);
        if(null == product || CollUtil.isEmpty(product.getColumnIds())){
            throw new BizRuntimeException(ExamResultCode.productNotExist);
        }
        List<ColumnModel> columnModels = columnService.selectBatchIds(product.getColumnIds());
        if(CollUtil.isEmpty(columnModels)){
            throw new BizRuntimeException(ExamResultCode.columnNotExist);
        }
        return columnModels;
    }

    @Override
    public List<ProductBaseModel> selectListByColumnId(Long categoryId,String columnId) {
//        "JSON_CONTAINS(sku_attr_ids, JSON_ARRAY("+str+"))";
        return this.selectList(Wrappers.<ProductBaseModel>lambdaQuery().eq(null != categoryId,ProductBaseModel::getCategoryId,categoryId)
                 .apply("JSON_CONTAINS(column_ids, CONCAT('\"'," + columnId + ",'\"'))"));
    }
}
